import jieba
from wordcloud import WordCloud
import csv
import operator

comment_list = []
word_dict = {}
words = []
with open("短评.csv","r") as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        comment_list.append(row[2])
comments = "".join(comment_list)
seg_list = list(jieba.cut(comments))

for item in seg_list:
    if len(item) < 2:
        word_dict[item] = 0
    if not item in word_dict:
        word_dict[item] = 1
    else:
        word_dict[item] += 1

times = sorted(word_dict.items(),key=operator.itemgetter(1),reverse=True)
with open("word_list.csv","w",newline="") as csvfile:
    writer = csv.writer(csvfile)
    for item in times:
        writer.writerow([item[0]],item[1])

i = 0
while times[i][1] >= 10:
    words.append(times[i][0])
    i += 1
result = "".join(words)

wc = WordCloud(
    background_color="white",
    font_path="",
    width=1000,
    height=860
)

word_cloud = wc.generate(result)
word_cloud.to_file("wordcloud.jpg")